package com.administrator.cloudapplication.activitys;


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.administrator.cloudapplication.R;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;

/**
 * 地图页面，显示地图并定位用户点击的地方，还要在上边添加一个红色的表示箭头。
 */

public class MapActivity extends AppCompatActivity {
    MapView mMapView;//地图的控件实例化
    AMap aMap;//地图控制器
    double xcor, ycor;//经纬度
    LatLng library;//定义一个坐标

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_baidu);
        //获取地图控件引用
        mMapView = (MapView) findViewById(R.id.map);
        //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState)，创建地图,此方法必须重写
        mMapView.onCreate(savedInstanceState);
        if (aMap == null) {
            //获取地图控制器
            aMap = mMapView.getMap();
        }
        //接收上个页面传过来的经纬度，放到坐标里
        library = new LatLng(Double.parseDouble(getIntent().getStringExtra("xcor")),
                ycor = Double.parseDouble(getIntent().getStringExtra("ycor")));
        //将地图中心用方法changeCamera移动到写好的坐标里
        changeCamera(
                CameraUpdateFactory.newCameraPosition(new CameraPosition(library, 18, 30, 30)));
        //为写好的坐标添加Marker标记
        aMap.addMarker(new MarkerOptions().position(new LatLng(ycor, xcor))
                .icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
    }

    /**
     * 根据动画按钮状态，调用函数animateCamera或moveCamera来改变可视区域
     */
    private void changeCamera(CameraUpdate update) {
        aMap.moveCamera(update);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mMapView.onPause();
    }
}
